/**
 * Created with IntelliJ IDEA.
 * Description:链表中倒数第k个节点
 * User: wangxin
 * Date: 2024-10-22
 * Time: 21:36
 */
import java.util.*;
public class test {



     public class ListNode {
        int val;
        ListNode next = null;
        public ListNode(int val) {
          this.val = val;
        }
      }



        public ListNode FindKthToTail (ListNode pHead, int k) {
            int n = 0;
            ListNode fast = pHead;
            ListNode slow = pHead;
            //快指针先行k步
            for(int i = 0; i < k; i++){
                if(fast != null)
                    fast = fast.next;
                    //达不到k步说明链表过短，没有倒数k
                else
                    return slow = null;
            }
            //快慢指针同步，快指针先到底，慢指针指向倒数第k个
            while(fast != null){
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
        }


}
